<template>
  <el-form
    ref="deptForm"
    label-width="120px"
    :model="form"
    :rules="rules"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manger">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in List" :key="item.id" :value="item.username" :label="item.username" />

      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getsimple, addDepartment, getDepartment, updataDepartment } from '@/api/employees.js'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isedit: {
      type: Boolean,
      required: true
    },
    // 用来校验部门编码，部门名称
    origin: {
      type: Array,
      required: true
    }
  },
  data() {
    // 定义一个自定义校验函数 校验部门编码code
    const valicode = (rule, value, callback) => {
      // 验证用户输入的value值，在编码中是否存在
      // 获取整个部门的编码，编码数组
      const codeList = this.origin.map(item => item.code)
      console.log(codeList, 'codeList')
      // 编辑状态下部门编码不能相同
      if (this.isedit) {
        this.origin.filter(item => item.id !== this.id).map(item => item.code)
      }
      // 判断有没有相同code吗
      if (codeList.includes(value)) {
        callback(new Error(`编码${value}存在`))
      } else {
        callback()
      }
    }
    // 校验部门名称不能重复
    const valiName = (rule, value, callback) => {
      // 得到有有子组件部门的名称
      let pidList = this.origin.filter(item => item.pid === this.id).map(item => item.name)
      console.log('pidList', pidList)
      if (this.isedit) {
        // 得到所有的有子组件的pid
        const pid = this.origin.find(item => item.id === this.id).pid
        // 从数组中origin取出当前所在的子组件的所有pid
        const brothers = this.origin.filter(item => item.pid === pid)
        // 当前的部门名字不能是所有origin里面已有的名字
        pidList = brothers.filter(item => item.id !== this.id).map(item => item.name)
      }
      if (pidList.includes(value)) {
        callback(new Error(`该部门${value}已存在)`))
      } else {
        callback()
      }
    }
    return {
      List: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: '1', max: '50', message: '长度在 1 到 5 个字符', trigger: 'blur' },
          { validator: valiName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: '1', max: '50', message: '长度在 1 到 5 个字符', trigger: 'blur' },
          { validator: valicode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'change' }

        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: '1', max: '50', message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.Loadsimple()
    if (this.isedit) {
      this.loadDepartment()
    }
  },
  methods: {
    // 重置表单验证
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 修改部门详情
    async doEdit() {
      try {
        await updataDepartment(this.form)
        this.$emit('opanclose')
      } catch (error) {
        console.log(error)
      }
    },

    // 获取部门详情数据
    async loadDepartment() {
      const res = await getDepartment(this.id)
      console.log(res, 'getDepartment')
      this.form = res.data
    },
    // 新增子部门
    async doAdd() {
      const res = await addDepartment({ pid: this.id, ...this.form })
      console.log(res, 'doadd')
    },
    // 获取员工简单列表
    async Loadsimple() {
      const res = await getsimple()
      console.log(res, 'Loadsimple')
      this.List = res.data
    },
    // 表单校验
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return alert('验证失败')
        this.$emit('opanclose')
        // 调用添加函数
        // this.doAdd()
        this.isedit ? this.doEdit() : this.doAdd()
      })
    },
    // 关闭弹层
    hCancel() {
      this.$emit('opanclose')
    }
  }
}
</script>

<style>

</style>
